from pywinauto.application import Application
from psutil import process_iter
from pywinauto import mouse

import pytest
import threading
import time,datetime,sys
from selenium import webdriver
import unittest

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import traceback

from tools import *


class ParametrizedTestCase(unittest.TestCase):
    """ TestCase classes that want to be parametrized should
        inherit from this class.
    """

    def __init__(self, methodName='runTest', param=None):
        super(ParametrizedTestCase, self).__init__(methodName)
        self.param = param

    @staticmethod
    def parametrize(testcase_class, param=None):
        """ Create a suite containing all tests taken from the given
            subclass, passing them the parameter 'param'.
        """
        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(testcase_class)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(testcase_class(name, param=param))
        return suite
class WeChat(ParametrizedTestCase):

    def connect_wechat(self):
        PID = self.get_pid()
        if PID == 0:
            print('未检测到微信运行')
            return 0
        else:
            # 在这里要设置应用后端，即 uia 或 win32，这里微信要设置为 uia，具体方法可参考文末链接
            self.app = Application(backend='uia')
            self.app.connect(process=PID)
            self.win = self.app[u'微信']
            print('已建立与微信链接')
        print()


    def sent_pic(self, channelShopId, path):
        self.connect_wechat()
        if self.win.child_window(title=channelShopId, control_type='Text').exists(timeout=1):
            self.click_one(title=channelShopId, control_type='Text')
            time.sleep(1)
        self.click_one(title="发送文件", control_type='Button')
        time.sleep(1)
        self.win.child_window(title="文件名(N):", class_name='Edit').type_keys(path)
        time.sleep(1)
        self.win.child_window(class_name='Button', title='打开(O)').click_input()
        time.sleep(1)
        self.click_one(title="发送(S)", control_type='Button')


    def get_pid(self):
        Application(backend='uia').start(r"C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe")
        PID = process_iter()
        for pid_temp in PID :
            pid_dic = pid_temp .as_dict(attrs=['pid','name'])
            if pid_dic['name'] == 'WeChat.exe':
                return pid_dic['pid']
        else:
            return 0

    # 左键点击某个控件
    def click_one(self, title='', control_type='', button='left', found_index=0, left=10, top=10, times=1):
        """
        :param title: 控件名称
        :param control_type: 控件类型
        :param button: left right middle 对应左、右、中三个键
        :param times: 点击次数
        :param found_index: 选择同名同类型多个控件中的第几个
        :param left: X 正方向修正
        :param top: Y 正方向修正
        :return:
        """
        # 在这里控件名称和类型，可以只选择一个，不过定位精准度不高
        if title == '' and control_type == '':
            raise ValueError('控件名和控件类型不可为空!')
        position = self.win.child_window(title=title, control_type=control_type, found_index=found_index).rectangle()
        for i in range(times):
            mouse.click(button=button, coords=(position.left + left, position.top + top))
            if times > 1:
                time.sleep(0.2)


    def test(self):
        str = "123...12"
        str1 = str[:-2]
        print()
